package org.dant.ant.extension.net.processors;

import java.util.*;
import java.io.*;

import org.apache.tools.ant.BuildException;

import org.dant.ant.extension.net.Processor;
import org.dant.ant.extension.net.DefaultDispatcher;
import org.dant.ant.extension.net.repository.ProcessGroup;
import org.dant.ant.extension.tasks.internal.RemoteCommand;
import org.dant.ant.extension.tasks.internal.RemoteResponse;
import org.dant.ant.extension.tasks.commands.CommandsConstants;
import org.dant.ant.extension.utils.ParameterUtil;

public class JavaProcessor extends Executable implements Processor {

  protected StartJavaApp setup(RemoteCommand rcmd) throws BuildException {
    StartJavaApp javaApp = new StartJavaApp();
    String[] cmdl = (String[]) rcmd.getParameter("javacommandline");
    if (cmdl == null) throw new BuildException("Command line can not be null");
    String[] args = (String[])rcmd.getParameter("arguments");
    boolean blocking = ((Boolean)rcmd.getParameter("blocking")).booleanValue();

    javaApp.setExecutable((String)rcmd.getParameter("classname")); // just set a non-null value, not very helpful really
    javaApp.setDir((File)rcmd.getParameter("dir"));
    javaApp.setCommandline(cmdl);
    //    javaApp.setArguments(args); //TODO maybe no use
    javaApp.setEnvironment((String[])rcmd.getParameter("enviroment"));
    javaApp.setExpectedList((List)rcmd.getParameter("expectedlist"));

    javaApp.setBlocking(blocking);
    javaApp.setUnique(((Boolean)rcmd.getParameter("unique")).booleanValue());
    javaApp.setDuplicate(((Integer)rcmd.getParameter("duplicate")).intValue());
    javaApp.setTimeout((Long)rcmd.getParameter("timeout"));
    javaApp.setFailonerror(((Boolean)rcmd.getParameter("failonerror")).booleanValue());
    return javaApp;
  }
  
  private void execute(RemoteCommand rcmd, RemoteResponse response) {
    StartJavaApp javaApp;
    
    try {
      javaApp = setup(rcmd);
    } catch(BuildException be) {
      response.setSuccess(false);
      response.setResult(be.getMessage());
      return;
    }
    
    String key = javaApp.getProcessKey();
    ProcessGroup group = DefaultDispatcher.getProcessRepository().getProcessGroup(key);
    
    if (group == null) {
      group = new ProcessGroup();
      group.setCommandLine(key);
      group.setProjectName((String)rcmd.getParameter("projectname"));
      group.setTargetName((String)rcmd.getParameter("targetname"));
      group.setTaskName((String)rcmd.getParameter("taskname"));
      
      group.setTestSuiteName((String)rcmd.getParameter("testsuitename"));
      group.setTestCaseName((String)rcmd.getParameter("testcasename"));
      
      group.setSourceHost(rcmd.getSourceHost());
      group.setBlocking(javaApp.getBlocking());
      DefaultDispatcher.getProcessRepository().addProcessGroup(key, group);
    }
    
    javaApp.setProcessGroup(group);

    javaApp.start(response);
  }
  
  public void process(RemoteCommand req, RemoteResponse response) {
    System.out.println("Java requied from : " + req.getSourceHost());
    response.setCommandType(req.getName());
    if (CommandsConstants.DANT_CMD_JAVA.equals(req.getName())) {
      execute(req, response);
    } else {
      response.setSuccess(false);
      response.setResult("Required a JAVA command , but is not a JAVA command");
    }
  }
}
